package summerbug.hanzi;

import android.app.Application;
import android.content.IntentFilter;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;


import java.util.List;

import summerbug.hanzi.base.BaseActivity;
import summerbug.hanzi.model.HanZiTable;

/**
 * Created by 76336 on 2018/5/12.
 */

public class ZiDianActivity extends BaseActivity implements View.OnClickListener{
    private ImageView iv;
    private TextView tv_bs;
    private TextView tv_pinyin;
    private TextView tv_py;
    private TextView tv_bh;
    private TextView tv_wb;
    private ImageView bt;
    private Receiver receiver;
    Boolean checked;
    List<HanZiTable> hanZiTableList;


    @Override
    protected int getLayout() {
        return R.layout.activity_zidian;
    }


    protected void initView() {
        iv = findViewById(R.id.iv_bs);
        tv_py = findViewById(R.id.cv_tv_py);
        tv_bs = findViewById(R.id.cv_tv_bs);
        tv_pinyin = findViewById(R.id.cv_tv_pinyin);
        tv_bh = findViewById(R.id.cv_tv_bh);
        bt = findViewById(R.id.collectBtn);
        tv_wb = findViewById(R.id.cv_tv_wb);

        setString("字典");

        bt.setOnClickListener(this);
        //Log.d("tag", "initView: "+getIntent().getExtras().getString("zd"));
    }

    @Override
    protected void onResume() {
        receiver = new Receiver(MyApplication.getInstance(),this);
        IntentFilter intentFilter = new IntentFilter("ACTION_REFRESH_UI");
        registerReceiver(receiver,intentFilter);
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    public void loadData(List<HanZiTable> hanZiTableList){
        this.hanZiTableList = hanZiTableList;
        for (HanZiTable tb : hanZiTableList){
            tv_py.setText("拼 音："+tb.getPy());
            tv_bs.setText("部 首："+tb.getBushou());
            tv_pinyin.setText("读 音："+tb.getPinyin());
            tv_bh.setText("笔 画："+tb.getBihua());
            tv_wb.setText("五 笔："+tb.getWubi());
            checked = tb.getChecked();
            Log.d("tag", "loadData: "+checked);
            GlideApp.with(this)
                    .load(tb.getImgurl())
                    .placeholder(R.mipmap.fy)
                    .into(iv);
        }
    }


    @Override
    public void onClick(View v) {
        if (!checked){
            Glide.with(ZiDianActivity.this)
                    .load(R.drawable.collected)
                    .into(bt);
            checked = true;
        }else {
            Glide.with(ZiDianActivity.this).load(R.drawable.collecting).into(bt);
            checked = false;
        }
        for (HanZiTable tb:hanZiTableList){
            tb.setChecked(checked);
        }
    }
}
